local fk__yuanxue = fk.CreateSkill {

  name = "fk__yuanxue",

  tags = {  },

}

local U = require "packages/utility/utility"


fk__yuanxue:addEffect(fk.TargetSpecified, {
  name = "fk__yuanxue",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fk__yuanxue.name) and U.isPureCard(data.card)
    and data:isOnlyTarget(data.to) and not data.to.dead
    and table.find(player:getPile("fk__jingzhu"), function (id)
      return Fk:getCardById(id).type == data.card.type
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    local ids = table.filter(U.getUniversalCards(room, "bt"), function (id)
      local card = Fk:getCardById(id)
      if card.skill:getMinTargetNum() > 1 then
        return false
      elseif card.skill:getMinTargetNum() == 0 then
        if to ~= player and not card.multiple_targets then return false end
      end
      return player:canUseTo(card, to, {bypass_distances = true, bypass_times = true})
    end)
    local cards = room:askForCard(player, 1, 1, false, fk__yuanxue.name, true, tostring(Exppattern{ id = ids }),
    "#fk__yuanxue-use::"..to.id..":"..data.card:getTypeString(), ids)
    if #cards > 0 then
      event:setCostData(self, Fk:getCardById(cards[1]).name)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pile = table.filter(player:getPile("fk__jingzhu"), function (id)
      return Fk:getCardById(id).type == data.card.type
    end)
    if #pile == 0 then return end
    if #pile > 1 then
      pile = {room:askForCardChosen(player, player, {card_data = { { "fk__jingzhu", pile } }}, fk__yuanxue.name, "#fk__yuanxue-remove")}
    end
    room:moveCards({
      ids = pile,
      from = player.id,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      skillName = fk__yuanxue.name,
      proposer = player,
    })
    room:useVirtualCard(event:getCostData(self), nil, player, data.to, fk__yuanxue.name, true)
  end,
})

return fk__yuanxue